package com.wwwarehouse.usercenter.fragment.permission_recycle;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.AuthorityDistributeRvAdapter;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthorityWrapItemBean;
import com.wwwarehouse.usercenter.bean.permission_recycle.AuthorizedOutItensBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by xiaoke.ni on 2017/10/11.
 */

public class PRListFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private List<AuthorizedOutItensBean.UserCardAuthoritiesBean> mDataList;
    private PRUserCardAuthorityAdapter mRvAdapter;

    private String mBusinessUnitId;

    public interface OnButtonClickedListener {
        void onButtonClicked();
    }

    private OnButtonClickedListener mButtonClickedListener;

    public void setOnButtonClickedListener(OnButtonClickedListener onButtonClickedListener) {
        mButtonClickedListener = onButtonClickedListener;
    }

    public static PRListFragment newInstance(ArrayList<AuthorizedOutItensBean.UserCardAuthoritiesBean> list, String mBusinessUnitId) {
        PRListFragment fragment = new PRListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(UserCenterConstant.KEY_AUTHORITY_RECYCLE_ITEM_BEAN, list);
        bundle.putString("businessId", mBusinessUnitId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDataList = bundle.getParcelableArrayList(UserCenterConstant.KEY_AUTHORITY_RECYCLE_ITEM_BEAN);
            mBusinessUnitId = bundle.getString("businessId");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_distribute_authority, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        // findViewById
        mRecyclerView = $(R.id.rv);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mRvAdapter = new PRUserCardAuthorityAdapter(mDataList, mRecyclerView.getHeight());
                mRvAdapter.setOnItemClickListener(new PRUserCardAuthorityAdapter.OnItemClickListener() {
                    @Override
                    public void onButtonClicked(int position) {
                        AuthorizedOutItensBean.UserCardAuthoritiesBean userCardAuthoritiesBean = mDataList.get(position);
                        if (mButtonClickedListener != null) {
                            mButtonClickedListener.onButtonClicked();
                        }
                        PRSelectPagerFragment fragment = new PRSelectPagerFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString("cardId", userCardAuthoritiesBean.getCardId() + "");
                        bundle.putString("businessId", mBusinessUnitId);
                        bundle.putString("cardName", userCardAuthoritiesBean.getCardName());
                        fragment.setArguments(bundle);
                        pushFragment(fragment);
//                        mRvAdapter.notifyDataSetChanged();
                    }
                });
                mRecyclerView.setAdapter(mRvAdapter);
                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

    }


    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
